import requests
import json
import math


def Get_tenant_token():
    # 获取app_token
    tenant_token_url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
    header = {
        'Content-Type': 'application/json; charset=utf-8'}
    tenant_token_form = {
        'app_id': 'cli_a6c9981642bd1013',
        'app_secret': 'kUFzBVbKpb2rFUZ9iQvU1cTGUZcLLrfx'}
    tenant_token_res = requests.post(tenant_token_url, headers=header, data=json.dumps(tenant_token_form))
    # print(tenant_token_res.text)
    tenant_token = json.loads(tenant_token_res.text)['tenant_access_token']
    # print('tenant_token : '+tenant_token)
    return tenant_token


# 标准电子表格
# 数据读取
def sheet_record_read(tenant_token, spreadsheetToken, sheetId, ranges):
    base_url = 'https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values_batch_get'.format(spreadsheetToken)
    header = {'Content-Type': 'application/json',
              'Authorization': 'bearer ' + tenant_token}
    get_data_param = '?valueRenderOption=UnformattedValue&dateTimeRenderOption=FormattedString&ranges=' + sheetId + '!' + ranges
    range_url = base_url + get_data_param
    get_data_resp = requests.get(url=range_url, headers=header)
    # print(get_data_resp.text)
    resp = json.loads(get_data_resp.content)
    if resp['code'] != 0:
        print(resp)
        return []
    resp_data = resp['data']
    link_data = resp_data['valueRanges'][0]['values']

    return link_data

